<?php

namespace App\Http\Controllers\Order;

use App\Http\Controllers\BaseController;
use App\Http\Validate\Order\SaleorderotherfeeSub as ValidateSaleorderotherfeeSub;
use App\Models\Manger\Manger;
use App\Models\Order\SaleorderOtherfeePayApprove;
use App\Models\Order\Saleordersub;
use App\Models\Order\Salesordersub;
use App\Models\Order\Saleorderfee as ModelSaleorderfee;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\Order\Saleorder as ModelSaleorder;

class SaleorderotherfeeController extends BaseController
{

    public function getlist(Request $request)
    {
        // if ($this->mangerinfo['utype'] != 7) {
        //     return response()->json(['code' => '001', 'msg' => '您无权限操作']);
        // }
        $isgetall = $request->input('isgetall');
        $list = ModelSaleorderfee::getList($request, $this->mangerinfo, $isgetall);

        return response()->json(['code' => '000', 'data' => $list]);
    }

    public function getlistCn(Request $request)
    {
        if ($this->mangerinfo['utype'] != 8) {
            return response()->json(['code' => '001', 'msg' => '您无权限操作']);
        }
        $isgetall = $request->input('isgetall');
        $list = ModelSaleorderfee::getListBycn($request, $this->mangerinfo, $isgetall);

        return response()->json(['code' => '000', 'data' => $list]);
    }


    public function getlistTs(Request $request)
    {
        if ($this->mangerinfo['utype'] != 4) {
            return response()->json(['code' => '001', 'msg' => '您无权限操作']);
        }

        $isgetall = $request->input('isgetall');
        $list = ModelSaleorderfee::getListByTs($request, $this->mangerinfo, $isgetall);

        return response()->json(['code' => '000', 'data' => $list]);
    }


    public function getlistCp(Request $request)
    {

        if ($this->mangerinfo['utype'] != 2) {
            return response()->json(['code' => '001', 'msg' => '您无权限操作']);
        }

        $isgetall = $request->input('isgetall');
        $list = ModelSaleorderfee::getListByCp($request, $this->mangerinfo, $isgetall);

        return response()->json(['code' => '000', 'data' => $list]);
    }




    public function getinfo(Request $request)
    {
        $cpid = $this->mangerinfo['cpid'];

        $otherfee_id = intval($request->otherfee_id);
        $otherfeeinfo = ModelSaleorderfee::where(['cpid' => $cpid, 'id' => $otherfee_id])->first();
        if (!$otherfeeinfo) {
            return response()->json(['code' => '001', 'msg' => '费用信息不存在']);
        }

        $list= ModelSaleorderfee::with(['tsinfo', 'cpinfo', 'pminfo', 'feeinfo','paybank','approve'])->where(['cpid' => $cpid, 'id' => $otherfee_id])
        ->where('pay_mid','<>',0)->orderBy('id','desc')->get();



        return response()->json(['code' => '000', 'data' => ['list' => $list]]);
    }
    public function getoinfo(Request $request)
    {
        $cpid = $this->mangerinfo['cpid'];

        $otherfee_id = intval($request->otherfee_id);
        $otherfeeinfo = ModelSaleorderfee::where(['cpid' => $cpid, 'id' => $otherfee_id])->first();

        if (!$otherfeeinfo) {
            return response()->json(['code' => '001', 'msg' => '费用信息不存在']);
        }
        $one = ModelSaleorder::with('suborder')->where(['cpid' => $cpid, 'order_number' => $otherfeeinfo->order_number])->first();
        $one = $one->toArray();
        if ($one['suborder']) foreach ($one['suborder'] as $key => $value) {
            $one['suborder'][$key]['weight'] = !empty($value['weight']) ? $value['weight'] / 10000 : 0;
            $one['suborder'][$key]['total_weight'] = !empty($value['total_weight']) ? $value['total_weight'] / 10000 : 0;
            $one['suborder'][$key]['unit_price'] = !empty($value['unit_price']) ? $value['unit_price'] / 100 : 0;
            $one['suborder'][$key]['total'] = !empty($value['total']) ? $value['total'] / 100 : 0;
            $one['suborder'][$key]['guiding_price'] = !empty($value['guiding_price']) ? $value['guiding_price'] / 100 : 0;
            $one['suborder'][$key]['accrual'] = !empty($value['accrual']) ? $value['accrual'] / 100 : 0;
            $one['suborder'][$key]['performance'] = !empty($value['performance']) ? $value['performance'] / 100 : 0;
        }
        $one['feeinfo'] = $otherfeeinfo;
        return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => $one]);
    }


    public function docancel(Request $request)
    {
        $cpid = $this->mangerinfo['cpid'];

        $otherfee_id = intval($request->otherfee_id);//approve_id

        $approveInfo = SaleorderOtherfeePayApprove::where(['cpid' => $cpid, 'id' => $otherfee_id])->first();
        $otherfeeinfo = ModelSaleorderfee::where('id', $approveInfo['other_pid'])->first();

        if ($this->mangerinfo['utype'] != 7) {
            return response()->json(['code' => '001', 'msg' => '您无权限操作']);
        }
        if (!$otherfeeinfo) {
            return response()->json(['code' => '001', 'msg' => '费用信息不存在']);
        }
        if ($approveInfo->is_pay != 2) {
            return response()->json(['code' => '001', 'msg' => '费用未支付']);
        }
        if ($approveInfo->status > 2) {
            return response()->json(['code' => '001', 'msg' => '此状态不允许撤审']);
        }
        $approveInfo->status = 5;
        $approveInfo->cs_at = time();
        $approveInfo->save();

//        $data[] = [
//            'cpid' => $cpid,
//            'order_number'=>$otherfeeinfo->order_number,
//            'fee_type_id' => $otherfeeinfo->fee_type_id,
//            'fee_type_name' => $otherfeeinfo->fee_type_name,
//            'fee_date' => $otherfeeinfo->fee_date,
//            'fee_id' => $otherfeeinfo->fee_id,
//            'fee_name' => $otherfeeinfo->fee_name,
//            'company_name' => $otherfeeinfo->company_name,
//            'price' => $otherfeeinfo->price,
//            'remark' => $otherfeeinfo->remark,
//            'other_pid'=>$otherfeeinfo->other_pid,
//        ];
//        ModelSaleorderfee::insert($data);
        return response()->json(['code' => '000', 'msg' => '撤审成功']);
    }
    /****
     *  销售费用审核，
     *  统审1->2 通过，1->4 驳回
     *  操盘2->3 通过，2->4 驳回，重新插入了一条 其他费用单？
     *
    */
    public function changetstatus(Request $request)
    {
        $cpid = $this->mangerinfo['cpid'];
        $otherfee_id = intval($request->otherfee_id);

        $status = intval($request->status);
        $otherfeeinfo = ModelSaleorderfee::where(['cpid' => $cpid, 'id' => $otherfee_id])->first();

        if (!$otherfeeinfo) {
            return response()->json(['code' => '001', 'msg' => '费用信息不存在']);
        }
        if ($otherfeeinfo->is_pay != 2) {
            return response()->json(['code' => '001', 'msg' => '费用未支付']);
        }
        if ($otherfeeinfo->status != 1) {
            return response()->json(['code' => '001', 'msg' => '此状态不允许审核']);
        }
        if ($status == 1) {
            $otherfeeinfo->status = 2;
        } else {
            $otherfeeinfo->status = 4;
        }
        $otherfeeinfo->ts_at = time();
        $otherfeeinfo->save();

//        if($otherfeeinfo->status==4){
//            $data[] = [
//                'cpid' => $cpid,
//                'order_number'=>$otherfeeinfo->order_number,
//                'fee_type_id' => $otherfeeinfo->fee_type_id,
//                'fee_type_name' => $otherfeeinfo->fee_type_name,
//                'fee_date' => $otherfeeinfo->fee_date,
//                'fee_id' => $otherfeeinfo->fee_id,
//                'fee_name' => $otherfeeinfo->fee_name,
//                'company_name' => $otherfeeinfo->company_name,
//                'price' => $otherfeeinfo->price,
//                'remark' => $otherfeeinfo->remark,
//                'other_pid'=>$otherfeeinfo->other_pid,
//            ];
//            ModelSaleorderfee::insert($data);
//        }


        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }

    public function changecstatus(Request $request)
    {
        $cpid = $this->mangerinfo['cpid'];
        $otherfee_id = intval($request->otherfee_id);

        $status = intval($request->status);
        $otherfeeinfo = ModelSaleorderfee::where(['cpid' => $cpid, 'id' => $otherfee_id])->first();

        if (!$otherfeeinfo) {
            return response()->json(['code' => '001', 'msg' => '费用信息不存在']);
        }
        if ($otherfeeinfo->is_pay != 2) {
            return response()->json(['code' => '001', 'msg' => '费用未支付']);
        }
        if ($otherfeeinfo->status != 2) {
            return response()->json(['code' => '001', 'msg' => '此状态不允许审核']);
        }
        if ($status == 1) {
            $otherfeeinfo->status = 3;
        } else {
            $otherfeeinfo->status = 4;
        }
        $otherfeeinfo->cp_at = time();
        $otherfeeinfo->save();
//
//        if($otherfeeinfo->status==4){
//            $data[] = [
//                'cpid' => $cpid,
//                'order_number'=>$otherfeeinfo->order_number,
//                'fee_type_id' => $otherfeeinfo->fee_type_id,
//                'fee_type_name' => $otherfeeinfo->fee_type_name,
//                'fee_date' => $otherfeeinfo->fee_date,
//                'fee_id' => $otherfeeinfo->fee_id,
//                'fee_name' => $otherfeeinfo->fee_name,
//                'company_name' => $otherfeeinfo->company_name,
//                'price' => $otherfeeinfo->price,
//                'remark' => $otherfeeinfo->remark,
//                'other_pid'=>$otherfeeinfo->other_pid,
//            ];
//            ModelSaleorderfee::insert($data);
//        }

        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }

    public function doreapay(Request $request)
    {
        $cpid = $this->mangerinfo['cpid'];
        $req_data = $request->only([
            'id',
            'bank_name',
            'pay_money',
            'pay_at',
            'screenshot_file',
            'remark'
        ]);

        $Validate = new ValidateSaleorderotherfeeSub();
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        $info = ModelSaleorderfee::where(['cpid' => $cpid, 'is_del' => 1, 'id' => $req_data['id']])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '无效参数']);
        }

        if ($info->status != 3) {
            return response()->json(['code' => '001', 'msg' => '审核成功方可付款']);
        }
        $data = [
            'pid' => $req_data['id'],
            'bank_name' => $req_data['bank_name'],
            'pay_money' => $req_data['pay_money'],
            'screenshot_file' => $req_data['screenshot_file'],
            'remark' => isset($req_data['remark']) ? $req_data['remark'] : '',
            'create_at' => time(),
        ];
        DB::table('sale_order_otherfee_sub')->insert($data);

        ModelSaleorderfee::where(['cpid' => $cpid, 'is_del' => 1, 'id' => $req_data['id']])->increment('pay_money_real', $data['pay_money']);


        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }


    public function getsublist(Request $request)
    {
        $cpid = $this->mangerinfo['cpid'];
        $otherfee_id = intval($request->otherfee_id);
        $approveInfo = SaleorderOtherfeePayApprove::where('id',$otherfee_id)->first();
        $otherfeeinfo = ModelSaleorderfee::where(['cpid' => $cpid, 'id' => $approveInfo['other_pid']])->first();

        if (!$otherfeeinfo) {
            return response()->json(['code' => '001', 'msg' => '费用信息不存在']);
        }
        if ($approveInfo->is_pay != 2) {
            return response()->json(['code' => '001', 'msg' => '费用未支付']);
        }

        if ($approveInfo->status != 3) {
            return response()->json(['code' => '001', 'msg' => '审批完成方可查看']);
        }



        $list = DB::table('sale_order_otherfee_sub')->where(['pid' => $otherfee_id])->get();

        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $list]);
    }
}
